import { Product } from '../../pages/product/product-model.js';
var product = new Product();
import { Cart } from '../../pages/cart/cart-model.js';
var cart = new Cart();
Component({
  properties: {
    list: {
      type: Array,
      value: ''
    },
    col:{
      type:Number,
      value:1
    },
    border:{
      type:Boolean,
      value:true
    },
    small:{
      type:Boolean,
      value:false
    }
  },
  data: {
    hiddenAuth:true,
    hiddenAttrBox:true
  },
  ready: function () {
  },
  methods: {
    //商品属性弹出框显示
    showattrbox: function (e) {
      var product_id = e.currentTarget.dataset.id;
      var index = e.currentTarget.dataset.index;
      this.data.index = index;
      this.setData({
        'hiddenAttrBox': false,
        'productCounts': 1,
      });
      product.getSkuData(product_id, (sku) => {
        this.setData({
          'productDetail': sku,
          'allselect': 0
        });
      })
    },
    //属性弹窗子组件返回来的值
    setnum:function(e){
      var that = this;
      var index = this.data.index;
      var total = e.detail.num;
      var item = 'list[' + index + '].cart_num';
      that.setData({
        [item] : total
      })
    },
    //购物车数量-加
    addCart: function(e){
      var that = this;
      var product_id = e.currentTarget.dataset.id;
      var sku_id = e.currentTarget.dataset.sku_id;
      var index = e.currentTarget.dataset.index;
      var param = {
        sku_id : sku_id,
        num : 1,
      }
      cart.addProduct2Cart(param, (data) => {
        if(data.status == 1){
          var item = 'list[' + index + '].cart_num';
          that.setData({
            [item] : data.num
          })
          wx.showToast({
            title: '添加成功',
            icon:'none'
          })
        }else{
          wx.showToast({
            title: data.msg ? data.msg : '操作失败',
            icon:'none'
          })
        }
      })
    },
    //手动输入商品数目
  numChange: function (e) {
    var that = this;
    var currentNum = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var sku_id = e.currentTarget.dataset.sku_id;
    var maxCount = this.data.list[index].skus[0].stock;
    if (currentNum && currentNum < 0) {
      currentNum = 1;
      wx.showToast({
        title: '数量必须是正整数',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (currentNum > maxCount) {
      currentNum = maxCount;
      if (maxCount == 0) {
        currentNum = 1;
        wx.showToast({
          title: '暂无库存~',
          icon: 'none',
          duration: 2000
        })
        return;
      } else {
        wx.showToast({
          title: '不能大于库存',
          icon: 'none',
          duration: 2000
        })
        return;
      }
    }
    var param={
      sku_id : sku_id,
      num : currentNum,
      retry : 1,
      is_self:1
    }
    if(param.num <= 0 ){
      //删除购物车
      cart.delete(sku_id, (data) => {
        if(data.status == 1){
          var item = 'list[' + index + '].cart_num';
          that.setData({
          [item]: 0,
          })
        }
      })
    }else{
      cart.addProduct2Cart(param, (data) => {
        if (data.status == 1) {
          var item = 'list[' + index + '].cart_num';
          this.setData({
            [item]: data.num
          })
          wx.showToast({
            title: '修改数量成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      });
    }
  },
    //购物车数量-减
    decCart: function(e){
      var that = this;
      var product_id = e.currentTarget.dataset.id;
      var sku_id = e.currentTarget.dataset.sku_id;
      var index = e.currentTarget.dataset.index;
      var old = parseInt(that.data.list[index].cart_num);
      /* console.log("索引是"+index); */
      var param = {
        sku_id : sku_id,
        num : old-1,
        retry:1,
        type : 'dec'
      }
      cart.addProduct2Cart(param, (data) => {
        if(data.status == 1){
          var item = 'list[' + index + '].cart_num';
          that.setData({
           [item]: data.num,
          })
        }/* else{
          var item = 'list[' + index + '].cart_num';
          that.setData({
           [item]: 0,
          })
        } */
      })
      if(param.num <= 0 ){
        //删除购物车
        cart.delete(sku_id, (data) => {
          if(data.status == 1){
            var item = 'list[' + index + '].cart_num';
            that.setData({
            [item]: 0,
            })
          }else{
            wx.showToast({
              title: data.msg ? data.msg : '操作失败',
              icon:'none'
            })
          }
        })
      }else{
        cart.addProduct2Cart(param, (data) => {
          if(data.status == 1){
            var item = 'list[' + index + '].cart_num';
            that.setData({
            [item]: data.num,
            })
          }
        })
      }
    },
    /*跳转到商品详情*/
    onProductsItemTap: function (e) {
      var id = e.currentTarget.dataset.id;
      var uid = wx.getStorageSync('uid');
      wx.navigateTo({
        url: '../product/product?id=' + id
      })
    }
  }
})